/*
 * @(#)ControllerPanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.netexplode.jtunes.client.player.jTunes;
import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.player.PlayerException;
import com.netexplode.jtunes.model.player.PlayerManager;

/**
 * <code>ControllerPanel</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.6 $
 * @since 0.1
 */
public class ControllerPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private static final VolumeControl volumeControl = new VolumeControl();

	private final static Dimension MAX_DIMENSION = new Dimension(250, 60);

	private CollectionManager collectionManager;

	private PlayerManager playerManager;

	public void setCollectionManager(CollectionManager collectionManager) {
		this.collectionManager = collectionManager;
	}

	public void setPlayerManager(PlayerManager playerManager) {
		this.playerManager = playerManager;
	}

	public void init() {
		super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		// adding subpanes to control pane
		final PlayerControl control = new PlayerControl(collectionManager, playerManager);
		control.setAlignmentY(CENTER_ALIGNMENT);
		super.add(control);
		volumeControl.setAlignmentY(CENTER_ALIGNMENT);
		super.add(volumeControl);

		volumeControl.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
			}
		});

		super.setPreferredSize(MAX_DIMENSION);
		super.setMaximumSize(MAX_DIMENSION);
		super.setMinimumSize(MAX_DIMENSION);
		super.setAlignmentY(CENTER_ALIGNMENT);

	}

	public static void updateButtonText(boolean play) {
		PlayerControl.playButton.setText(play ? "Stop" : "Play");
	}

	public static int getVolumeValue() {
		return volumeControl.getValue();
	}

	/**
	 * <code>PlayerControl</code> represents .....
	 * 
	 * @author ykim
	 * @version $Revision: 1.6 $
	 * @since 0.1
	 */
	private static class PlayerControl extends JPanel {

		private static final long serialVersionUID = 1L;

		private static final JButton prevButton = new JButton("Prev");

		private static final JButton playButton = new JButton("Play");

		private static final JButton nextButton = new JButton("Next");

		private CollectionManager collectionManager;

		private PlayerManager playerManager;

		/**
		 * 
		 */
		private PlayerControl(CollectionManager collectionManager, PlayerManager playerManager) {
			super();
			this.playerManager = playerManager;
			this.collectionManager = collectionManager;
			initialize();
		}

		/**
		 * 
		 */
		private void initialize() {
			// set layout
			this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

			// action event handlers
			this.addPreviousActionListener();
			this.addPlayActionListener();
			this.addNextActionListener();

			// add to component
			this.add(prevButton);
			this.add(playButton);
			this.add(nextButton);
		}

		/**
		 * 
		 */
		private void addNextActionListener() {
			nextButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						final boolean playing = playerManager.isPlaying();

						if (playing) {
							playerManager.getMediaPlayer().stop();
						}

						collectionManager.moveNext();

						if (playing) {
							playerManager.getMediaPlayer().play();
						}
					}
					catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
		}

		/**
		 * 
		 */
		private void addPreviousActionListener() {
			prevButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						final boolean playing = playerManager.isPlaying();

						if (playing) {
							playerManager.getMediaPlayer().stop();
						}

						collectionManager.movePrevious();

						if (playing) {
							playerManager.getMediaPlayer().play();
						}
					}
					catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
		}

		/**
		 * 
		 */
		private void addPlayActionListener() {
			playButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					stop();
					play();
				}
			});
		}

		private void stop() {
			try {
				playerManager.getMediaPlayer().stop();
			}
			catch (PlayerException e1) {
				e1.printStackTrace();
			}
			playerManager.setPlaying(false);
		}

		private void play() {
			try {
				playerManager.getMediaPlayer().play();
			}
			catch (PlayerException e1) {
				e1.printStackTrace();
			}
			playerManager.setPlaying(true);
			jTunes.setLcdPanelText();
		}
	}

	/**
	 * <code>VolumeControl</code> represents .....
	 * 
	 * @author ykim
	 * @version $Revision: 1.6 $
	 * @since 0.1
	 */
	private static class VolumeControl extends JSlider {

		private static final long serialVersionUID = 1L;

	}
}